package com.ltw.playit.common.utils;


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonConverter {

	private static final Logger logger = LoggerFactory.getLogger(JsonConverter.class);
	
	private ObjectMapper mapper = new ObjectMapper();

	public String asString(Object object) {
		String result = StringUtils.EMPTY;
		try {
			result = mapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}
		return result;
	}
	
	public <T> T covertToObject(String str, Class<T> cls) {
		try {
			return mapper.readValue(str, cls);
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}
		return null;
	}

	private JsonConverter() {
	}

	public static JsonConverter instance = new JsonConverter();
}
